var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var streamqueue = require('streamqueue');
var path  = require('path');
var seq    = require('run-sequence');
var rimraf  = require('gulp-rimraf');

var paths = {
  dest: 'www',
  sass: ['./scss/**/*.scss'],
  libFiles : [
    './www'
  ],
  appFiles : [
  './src/js/filters/*.js','./src/js/app.js',
  './src/js/constant/**/*.js','./src/js/controllers/**/*.js',
  './src/js/services/**/*.js','./src/js/directives/**/*.js',
  './src/js/factory/*.js'
],
  vendor: {
    js: [
      'lib/ionic/js/angular/angular.min.js',
      'lib/ionic/js/angular/angular-animate.min.js',
      'lib/ionic/js/angular/angular-resource.min.js',
      'lib/ionic/js/angular/angular-sanitize.min.js',
      'lib/ionic/js/angular-ui/angular-ui-router.min.js',
      'lib/ionic/js/ionic.min.js',
      'lib/ionic/js/ionic.bundle.min.js',
      'lib/ionic/js/ionic-angular.min.js',
      'lib/jquery/dist/jquery.min.js',
      'lib/echarts/echarts.min.js'
    ],

    css: [

    ],

    fonts: [
      './lib/ionic/fonts/ionic*.*'
    ]
  }
};

gulp.task('build', function(done) {
  var tasks = ['clean','libcss','libjs','sass','appjs','fonts','templates','html','images'];
  seq('clean', tasks, done);
});

gulp.task('clean', function (cb) {
  return gulp.src([
      path.join(paths.dest, 'index.html'),
      path.join(paths.dest, 'images'),
      path.join(paths.dest, 'css'),
      path.join(paths.dest, 'js'),
      path.join(paths.dest, 'fonts'),
      path.join(paths.dest, 'templates')
    ], { read: false })
    .pipe(rimraf());
});


gulp.task('sass', function(done) {
  gulp.src(['./scss/lib.scss','./scss/main.scss','./scss/main/*.scss'])
    .pipe(sass())
    .on('error', sass.logError)
    .pipe(minifyCss({
      keepSpecialComments: 0
    }))
    .pipe(concat('lib.css'))
    .pipe(gulp.dest('./www/css/'))
    .on('end', done);
});

gulp.task('templates', function() {
  return gulp.src('src/templates/**/*')
    .pipe(gulp.dest('www/templates'));
});

gulp.task('html', function() {
  return gulp.src('src/index.html')
    .pipe(gulp.dest('www/'));
});

gulp.task('images', function () {
  return gulp.src('src/images/**/*')
    .pipe(gulp.dest(path.join(paths.dest, 'images')));
});

gulp.task('fonts', function() {
  return gulp.src(paths.vendor.fonts)
    .pipe(gulp.dest(path.join(paths.dest, 'fonts')));
});

gulp.task('watch', function() {
  gulp.watch(paths.sass, ['sass']);
  gulp.watch(paths.appFiles, ['appjs']);
});

gulp.task('install', ['git-check'], function() {
  return bower.commands.install()
    .on('log', function(data) {
      gutil.log('bower', gutil.colors.cyan(data.id), data.message);
    });
});

gulp.task('appjs', function() {
        gulp.src(paths.appFiles)
        .pipe(concat('app.js'))
        .pipe(gulp.dest('www/js'));
});

gulp.task('libjs', function() {
  streamqueue({ objectMode: true },
    gulp.src(paths.vendor.js)
  )
    .pipe(concat('lib.min.js'))
    .pipe(gulp.dest('www/js'));
});

gulp.task('libcss', function() {
  streamqueue({ objectMode: true },
    gulp.src(paths.vendor.css)
    .pipe(concat('lib.css'))
    .pipe(gulp.dest('www/css')));
});


gulp.task('git-check', function(done) {
  if (!sh.which('git')) {
    console.log(
      '  ' + gutil.colors.red('Git is not installed.'),
      '\n  Git, the version control system, is required to download Ionic.',
      '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
      '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
    );
    process.exit(1);
  }
  done();
});
